﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class KhachHangDaiChung_KhachHangDaiChung_ThemSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ckKhuyenMai.Checked = true;
            DataDSTableAdapters.PHANLOAITableAdapter pladapter = new DataDSTableAdapters.PHANLOAITableAdapter();
            DataDS.PHANLOAIDataTable pltable = pladapter.PHANLOAI_LayPhanLoaiTheoIDCha(0);
           
            TreeNode treenode = new TreeNode();
            treenode.Text = "Phân loại";
            treenode.Value = "0";
            treenode.PopulateOnDemand = true;
            treenode.SelectAction = TreeNodeSelectAction.SelectExpand;
            tvPhanLoai.Nodes.Add(treenode);
            

            //DataDSTableAdapters.HINHTHUC_KHUYENMAITableAdapter htkmAdapter = new DataDSTableAdapters.HINHTHUC_KHUYENMAITableAdapter();
            //ddlHinhThucKhuyenMai.DataSource = htkmAdapter.GetData();
            //ddlHinhThucKhuyenMai.DataBind();
            //btnThem.Attributes["onclick"] = "return checkGoOn();";
            

            

            
        }
        if (tvPhanLoai.SelectedValue != "" && tvPhanLoai.SelectedValue != "0")
        {
            //lbPhanLoai.Text = tvPhanLoai.SelectedValue;
            txtLoaiSP.Text = tvPhanLoai.SelectedNode.Text;
        }
        else
            txtLoaiSP.Text = "";

    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        DataDSTableAdapters.SANPHAMTableAdapter spadapter = new DataDSTableAdapters.SANPHAMTableAdapter();
        string masanpham = txtMaSanPham.Text;
        string tensanpham = txtTenSanPham.Text;
        string hinhanh = imageFolder + fileuploadHinhAnh.FileName;
        string mota = txtMoTa.Text;
        string nhasanxuat = txtNhaSanXuat.Text;
        int soluong =int.Parse( txtSoLuong.Text);
        bool trangthaiban;
        if(rbtTrangThaiBan1.Checked==true)
            trangthaiban = true;
        else
            trangthaiban=false;
        DateTime thoigiandang = DateTime.Today;
        bool duocyeuthich = false; // checkboxDuocYeuThich.Checked;
        //float giacu = float.Parse(txtGiaCu.Text.ToString());
        float gia = float.Parse(txtGiaMoi.Text);
        string donvitinh = comboboxDonViTinh.Text;
        int phanloai = int.Parse(tvPhanLoai.SelectedValue);
        int? idSP = 0;
        spadapter.SANPHAM_Them(masanpham, tensanpham, mota, hinhanh,nhasanxuat, soluong, trangthaiban, thoigiandang, duocyeuthich, gia, 1, donvitinh, phanloai, ref idSP);
        if (idSP == -1)
        {
            Response.Write("<script>alert('Mã sản phẩm đã tồn tại');</script>");
            return;
        }
        
        //luu lai de thong ke
        DataDSTableAdapters.GIA_SANPHAMTableAdapter gia_spAdapter = new DataDSTableAdapters.GIA_SANPHAMTableAdapter();
        gia_spAdapter.GIA_SANPHAM_Insert(idSP, DateTime.Now, gia);

        SaveFileUpload();

        //them thuoc tinh
        if (tvPhanLoai.SelectedValue != "")
        {
            string strconnection = ConfigurationManager.ConnectionStrings["SMOConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(strconnection);
            connection.Open();
            string query = "select tt.* " +
                           "from PHANLOAI_THUOCTINH pltt, THUOCTINH tt " +
                           "where pltt.Id_ThuocTinh=tt.Id and pltt.Id_PhanLoai=" + tvPhanLoai.SelectedValue;
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            command.CommandText = query;
            command.Connection = connection;
            System.Data.SqlClient.SqlDataReader dr = command.ExecuteReader();
            if (dr.RecordsAffected != 0)
            {
                DataDSTableAdapters.SANPHAM_THUOCTINHTableAdapter ttAdapter = new DataDSTableAdapters.SANPHAM_THUOCTINHTableAdapter();
                while (dr.Read())
                {
                    string id = dr["Id"].ToString();
                    string ten = dr["TenThuocTinh"].ToString();
                    string giatri = Request.Form.Get(id);
                    ttAdapter.CHITIET_THUOCTINH_Them(idSP, int.Parse(id), giatri);
                }

            }
        }
        //end them thuoc tinh


        //them khuyen mai
        if (ckKhuyenMai.Checked)
        {
            try
            {
                DateTime ngaybatdau = DateTime.Parse(txtNgayBatDau.Text);
                DateTime ngaykethuc = DateTime.Parse(txtNgayKetThuc.Text);
                string noidung = txtMoTaKhuyenMai.Text;
                int? idKhuyenMai = 0;
                DataDSTableAdapters.KHUYENMAITableAdapter kmAdapter = new DataDSTableAdapters.KHUYENMAITableAdapter();
                kmAdapter.KHUYENMAI_Them(ngaybatdau, ngaykethuc, noidung, idSP, ref idKhuyenMai);
                string str = Request.Form.Get("soluonghinhthuc");
                int soluongHinhThuc = int.Parse(str);
                DataDSTableAdapters.CHITIET_KHUYENMAITableAdapter ctkmAdapter = new DataDSTableAdapters.CHITIET_KHUYENMAITableAdapter();
                int giatrigiamgia = 0;
                if (txtGiamGia.Text != "")
                    giatrigiamgia = int.Parse(txtGiamGia.Text);
                ctkmAdapter.CHITIETKHUYENMAI_Them(idKhuyenMai, 2/*giam gia*/, giatrigiamgia);
                for (int i = 0; i < soluongHinhThuc; i++ )//soluonghinhthuc= so luong san pham tang kem
                {
                    //int hinhthuc = int.Parse(Request.Form.Get("jHinhThuc_"+i).ToString());
                    int giatri = int.Parse(Request.Form.Get("jGiaTri_" + i).ToString());                    
                    ctkmAdapter.CHITIETKHUYENMAI_Them(idKhuyenMai, 1/*tang kem sp*/, giatri);
                }
            }
            catch (System.Exception ex)
            {

            }
        }
        
        
        //end them khuyen mai

        Response.Write("<script>alert('Thêm sản phẩm thành công');location.href='Admin_QuanLySanPham.aspx'</script>");       
   
        
    }
    private void SaveFileUpload()
    {
        string strFilename;
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        if (fileuploadHinhAnh.FileName != "")
        {
            strFilename = Server.MapPath(imageFolder + fileuploadHinhAnh.FileName);
            fileuploadHinhAnh.PostedFile.SaveAs(strFilename);
        }
    }

    protected void tvPhanLoai_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        string idcha = e.Node.Value;
        DataDSTableAdapters.PHANLOAITableAdapter pladapter = new DataDSTableAdapters.PHANLOAITableAdapter();
        DataDS.PHANLOAIDataTable pltable = pladapter.PHANLOAI_LayPhanLoaiTheoIDCha(int.Parse(idcha));
        if (pltable.Rows.Count == 0)
            return;
        foreach (DataDS.PHANLOAIRow row in pltable)
        {
            TreeNode treenode = new TreeNode();
            treenode.Text = row["TenPhanLoai"].ToString();
            treenode.Value = row["Id"].ToString();
            treenode.PopulateOnDemand = true;
            treenode.SelectAction = TreeNodeSelectAction.SelectExpand;
            e.Node.ChildNodes.Add(treenode);

        }
    }
    protected void ckKhuyenMai_CheckedChanged(object sender, EventArgs e)
    {
        if (ckKhuyenMai.Checked)
        {
            txtNgayBatDau.Visible = true;
            txtNgayKetThuc.Visible = true;
            txtMoTaKhuyenMai.Visible = true;
            Panel1.Visible = true;
            lbMoTa.Visible = true;
            lbNgayBatDau.Visible = true;
            lbNgayKetThuc.Visible = true;
            lbSoLuongHinhThuc.Visible = true;
            lbGiamGia.Visible = true;
            txtGiamGia.Visible = true;
            lbPhanTram.Visible = true;
        }
        else
        {
            txtNgayBatDau.Visible = false;
            txtNgayKetThuc.Visible = false;
            txtMoTaKhuyenMai.Visible = false;
            Panel1.Visible = false;
            lbMoTa.Visible = false;
            lbNgayBatDau.Visible = false;
            lbNgayKetThuc.Visible = false;
            lbSoLuongHinhThuc.Visible = false;
            lbGiamGia.Visible = false;
            txtGiamGia.Visible = false;
            lbPhanTram.Visible = false;
        }
    }
}
